package org.example.service.impl;

import org.example.mapper.HospitalMapper;
import org.example.pojo.Doctor.Department;
import org.example.pojo.Doctor.Doctor;
import org.example.pojo.Doctor.Hospital;
import org.example.pojo.Doctor.OutpatientClinic;
import org.example.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class HospitalServiceImp implements HospitalService {
    @Autowired
    private HospitalMapper hospitalMapper ;
    @Override
    public List<Hospital> getAllHospitals() {
        return hospitalMapper.getAllHospitals();
    }
    //根据医院id获取部门信息
    @Override
    public List<Department> getDepartmentsByHospitalId(Integer hospitalId) {
        return hospitalMapper.getDepartmentsByHospitalId(hospitalId);
    }
    //获取所有部门信息
    @Override
    public List<Department> getAllDepartment() {
        return hospitalMapper.getAllDepartment();
    }
    //根据部门id获取门诊信息
    @Override
    public List<OutpatientClinic> getClinicsByDepartmentId(Integer departmentId) {
        return hospitalMapper.getClinicsByDepartmentId(departmentId);
    }
    //获取所有门诊信息
    @Override
    public List<OutpatientClinic> getAllClinics() {
        return hospitalMapper.getAllClinics();
    }
    //根据门诊id获取医生信息
    @Override
    public List<Doctor> getDoctorsByClinicId(Integer clinicId) {
        return hospitalMapper.getDoctorsByClinicId(clinicId);
    }
    @Override
    public void addHospital(Hospital hospital) {
        hospitalMapper.addHospital(hospital);
    }

    @Override
    public void addDepartment(Department department) {
        hospitalMapper.addDepartment(department);
    }

    @Override
    public void addClinic(OutpatientClinic clinic) {
        hospitalMapper.addClinic(clinic);
    }

    @Override
    public void addDoctor(Doctor doctor) {
        hospitalMapper.addDoctor(doctor);
    }

    @Override
    public Doctor findByUserName(String username) {
        return hospitalMapper.findByUserName(username);
    }
    //删除医院
    @Override
    public void deleteHospital(Integer hospitalId) {
        //删除医院的时候，应该保留科室和门诊以及医生，但是应该删除关联关系,删除医院，departments数据库中的hospital_id应该置为空
        Integer departmentId = hospitalMapper.getDepartmentIDByHospitalID(hospitalId);
        if (departmentId!= null) {
            hospitalMapper.alterDepartmentsByHospitalID(departmentId);
        }
        hospitalMapper.deleteHospital(hospitalId);
    }
    //删除科室
    @Override
    public void deleteDepartment(Integer departmentId) {
        //同理
        Integer clinicId = hospitalMapper.getClinicIDByDepartmentID(departmentId);
        if (clinicId!= null) {
            hospitalMapper.alterClinicsByDepartmentID(clinicId);
        }
        hospitalMapper.deleteDepartment(departmentId);
    }
    //删除门诊
    @Override
    public void deleteClinic(Integer clinicId) {
        Integer doctorId = hospitalMapper.getDoctorIDByClinicID(clinicId);
        if (doctorId!= null) {
            hospitalMapper.alterDoctorsByClinicID(doctorId);
        }
        hospitalMapper.deleteClinic(clinicId);
    }
    //删除医生
    @Override
    public void deleteDoctor(Integer doctorId) {
        hospitalMapper.deleteDoctor(doctorId);
    }
    //获取所有医生信息
    @Override
    public List<Doctor> getAllDoctors() {
        return hospitalMapper.getAllDoctors();
    }

    //查询医生的ID
    @Override
    public String findByID(Integer doctorId) {
        return hospitalMapper.findByID(doctorId);
    }
    //更新医院信息
    @Override
    public void updateHospital(Hospital hospital) {
        hospitalMapper.updateHospital(hospital);
    }
    //更新科室信息
    @Override
    public void updateClinic(OutpatientClinic clinic) {
        hospitalMapper.updateClinic(clinic);
    }
    //更新部门信息
    @Override
    public void updateDepartment(Department department) {
        hospitalMapper.updateDepartment(department);
    }
}
